package IdeaGui;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
//import java.util.Date;
import java.util.Hashtable;

/**
 * User: Mike
 * Date: 11/15/11
 */

public class ReadInData {
    private ArrayList<Double> tempTCArr = new ArrayList<Double>();                    // don't have to read in all the data for the type of plotting I'm doing now
    private ArrayList<Double> tempACArr = new ArrayList<Double>();
    private ArrayList<Double> kalmanCArr = new ArrayList<Double>();
    private ArrayList<Double> milliTimeArr = new ArrayList<Double>();
    private ArrayList<Double> boilerTemp = new ArrayList<Double>();
    private ArrayList<Double> corrAbv = new ArrayList<Double>();
    private ArrayList<Double> vaporAbvCorr = new ArrayList<Double>();
    //    private ArrayList<Double> bottleAbv = new ArrayList<Double>();
//    private ArrayList<Double> timeS = new ArrayList<Double>();
    private ArrayList<Double> vaporAbv = new ArrayList<Double>();
    private ArrayList<Integer> bottleNum = new ArrayList<Integer>();
    private ArrayList<Double> rateByRate = new ArrayList<Double>();
    private ArrayList<Double> bottleVol = new ArrayList<Double>();

    Hashtable<String, Double> bottleAbv = new Hashtable<String, Double>();
    //    public static ArrayList<Double> distTemp = new ArrayList<Double>();
    //    public static ArrayList<String> dateTime = new ArrayList<String>();
    //    public static ArrayList<Double> tempAFArr = new ArrayList<Double>();
    //    public static ArrayList<Double> tempTFArr = new ArrayList<Double>();

    private static long endTime = 0;
    private String timeSpan;

    private BottleCalcs bottleCalcs;


    MainGui mainGui;

    public ReadInData(MainGui mainGui, BottleCalcs bottleCalcs) {
        this.mainGui = mainGui;
        this.bottleCalcs = bottleCalcs;
    }

    private String getTimeSpanHash() {
        Hashtable<String, String> hashtable = new Hashtable<String, String>();
        hashtable.put("5 min", "5"); //adding value to Hashtable
        hashtable.put("10 min", "10");
        hashtable.put("20 min", "20");
        hashtable.put("30 min", "30");
        hashtable.put("45 min", "45");
        hashtable.put("1 hr", "60");
        hashtable.put("2 hr", "120");
        hashtable.put("5 hr", "300");
        hashtable.put("10 hr", "600");
        hashtable.put("1 day", "1440");
        hashtable.put("2 days", "2880");
        hashtable.put("3 days", "4320");
        hashtable.put("4 days", "5760");
        hashtable.put("5 days", "7200");
        hashtable.put("6 days", "8640");
        hashtable.put("7 days", "10080");
        hashtable.put("8 days", "11520");
        hashtable.put("10 days", "14400");
        hashtable.put("30 days", "43200");
//        return hashtable.get(Data.timeSpan);
        return hashtable.get(timeSpan);
    }

    //    private void convertTime(int milliTime) {
//        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
//        Date resultdate = new Date(milliTime);
//        System.out.println(sdf.format(resultdate));
//    }

    public void Read() {
        BufferedReader readbuffer = null;
        String filePathName = "C:\\WhiskeyData\\output.txt";

        try {
            readbuffer = new BufferedReader(new FileReader(filePathName));      //	"" + DatFile.Path[DatFile.Index] + "\\" + DatFile.Name[DatFile.Index]));
        } catch (FileNotFoundException e1) {
            System.out.println("File not found: " + filePathName);
            System.out.println("Cannot display data, none recorded yet.");

//            e1.printStackTrace();
        }
        String strRead;
        long startReadHere = 0;
        int i = 0;                         // where we are in the file
        int k = 0;                         // index to import next set of data aquisition initialization
        int lastRecordedBottleNum = 0;

        long totalMin = CountData();
        if (totalMin > Long.valueOf(getTimeSpanHash())) {
            startReadHere = (endTime - 60 * 1000 * Long.valueOf(getTimeSpanHash()));
        }
        try {
            if (readbuffer != null)
                while ((strRead = readbuffer.readLine()) != null) {
                    String splitarray[] = strRead.split("\t");
                    if (k == 0) {        // header
                        cleanUpArrays();
                    }
                    if (k >= 1) {
                        if (Double.parseDouble(splitarray[1].trim()) >= startReadHere) {
                            if (splitarray.length >= 8) {
                                milliTimeArr.add(Double.parseDouble(splitarray[1].trim()));
                                if (splitarray.length >= 2)
                                    kalmanCArr.add(Double.parseDouble(splitarray[2].trim()));
                                if (splitarray.length >= 3)
                                    tempTCArr.add(Double.parseDouble(splitarray[3].trim()));
                                if (splitarray.length >= 5)
                                    tempACArr.add(Double.parseDouble(splitarray[5].trim()));
                                if (splitarray.length >= 7)
                                    bottleNum.add(Integer.parseInt(splitarray[7].trim()));
                                if (splitarray.length >= 8)
                                    corrAbv.add(Double.parseDouble(splitarray[8].trim()));
                                if (splitarray.length >= 9)
                                    vaporAbv.add(Double.parseDouble(splitarray[9].trim()));
                                if (splitarray.length >= 11)
                                    boilerTemp.add(Double.parseDouble(splitarray[11].trim()));
                                if (splitarray.length >= 12)
                                    rateByRate.add(Double.parseDouble(splitarray[12].trim()));
                                if (splitarray.length >= 15)
                                    bottleVol.add(Double.parseDouble(splitarray[15].trim()));
                                if (splitarray.length >= 19)
                                    vaporAbvCorr.add(Double.parseDouble(splitarray[19].trim()));

                                if (lastRecordedBottleNum != bottleNum.get(bottleNum.size() - 1)) {
                                    bottleCalcs.newBottle(bottleNum.get(bottleNum.size() - 1), rateByRate.get(bottleNum.size() - 1), corrAbv.get(bottleNum.size() - 1), bottleVol.get(bottleNum.size() - 1));
//                                newBottle(int bottleNum, double rate2, double abv2)
                                }

                                if (bottleAbv.size() == 0) {
//                                bottleAbv.put(String.valueOf(bottleNum.get(bottleNum.size() - 1)), corrAbv.get(corrAbv.size()-1));
                                    bottleAbv.put(String.valueOf(lastRecordedBottleNum + 1), corrAbv.get(corrAbv.size() - 1));
                                    lastRecordedBottleNum = bottleNum.get(bottleNum.size() - 1);
                                }

                                // if the bottleNum num has changed by 2, record the previous bottleNum corrAbv
                                if (bottleNum.size() > 1 && bottleNum.get(bottleNum.size() - 1) - lastRecordedBottleNum == 3) {
                                    Double changeAbv = corrAbv.get(corrAbv.size() - 1) - bottleAbv.get(String.valueOf(lastRecordedBottleNum));
                                    changeAbv = bottleAbv.get(String.valueOf(lastRecordedBottleNum)) + changeAbv / 3.;
                                    bottleAbv.put(String.valueOf(lastRecordedBottleNum + 1), changeAbv);
                                    lastRecordedBottleNum++;
                                }

                                // if the bottleNum num has changed by 2, record the previous bottleNum corrAbv
                                if (bottleNum.size() > 1 && bottleNum.get(bottleNum.size() - 1) - lastRecordedBottleNum == 2) {
                                    Double changeAbv = corrAbv.get(corrAbv.size() - 1) - bottleAbv.get(String.valueOf(lastRecordedBottleNum - 1));
                                    changeAbv = bottleAbv.get(String.valueOf(lastRecordedBottleNum)) + changeAbv / 2.;
                                    bottleAbv.put(String.valueOf(lastRecordedBottleNum + 1), changeAbv);
                                    lastRecordedBottleNum++;
                                }

                                // if the bottleNum num has changed by 1, record new bottleNum corrAbv
                                if (bottleNum.size() > 1 && bottleNum.get(bottleNum.size() - 1) - lastRecordedBottleNum == 1) {
                                    bottleAbv.put(String.valueOf(lastRecordedBottleNum + 1), corrAbv.get(corrAbv.size() - 1));
                                    lastRecordedBottleNum = bottleNum.get(bottleNum.size() - 1);
                                }


                                //                        distTemp.add(Double.parseDouble(splitarray[10].trim()));
                                //                        tempAFArr.add(Double.parseDouble(splitarray[6].trim()));
                                //                        tempTFArr.add(Double.parseDouble(splitarray[4].trim()));
                                //                        timeS.add(Double.parseDouble(splitarray[2].trim()));
                                //                        dateTime.add(splitarray[0].trim());
                            }
                        }
                    }
                    k = k + 1;
                    i = i + 1;
                }
            if (readbuffer != null)
                readbuffer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static long CountData() {
        System.out.println("Reading in DAT file....");                                    // import data from file (DatFile.Index), line by line (i), into arrays (j)
        BufferedReader readbuffer = null;
        String filePathName = "C:\\WhiskeyData\\output.txt";
        try {

            readbuffer = new BufferedReader(new FileReader(filePathName));
        } catch (FileNotFoundException e1) {
            System.out.println("File not found:" + filePathName);
            System.out.println("Cannot display data, none found yet.");
//            e1.printStackTrace();
        }
        String strRead;
        int i = 0;                         // where we are in the file
        int k = 0;                         // index to import next set of data aquisition initialization
        long initialMin = 0;
        long totalMin = 0;
        boolean firstLine = true;
        try {
            assert readbuffer != null;
            if (readbuffer != null)
                while ((strRead = readbuffer.readLine()) != null) {
                    String splitarray[] = strRead.split("\t");
                    if (k == 0)         // header
                        System.out.println(Arrays.toString(splitarray));
                    if (k > 0) {        // read in data
                        if (splitarray.length >= 12) {                         // check to make sure there is data there
                            if (firstLine) {
                                firstLine = false;
                                initialMin = (long) Double.parseDouble(splitarray[1].trim());
                            }
                            endTime = (long) Double.parseDouble(splitarray[1].trim());
                        }
                    }
                    k = k + 1;
                    i = i + 1;
                }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            if (readbuffer != null)
                readbuffer.close();
        }                         //Close the stream to free memory resources on the host computer:
        catch (IOException e1) {
            e1.printStackTrace();
        }

        System.out.println("totalMin readin = " + totalMin);
        totalMin = (endTime - initialMin) / 1000 / 60;
        System.out.println("totalMin readin = " + totalMin);
        return totalMin;
    }

    private void cleanUpArrays() {
        milliTimeArr.clear();
        kalmanCArr.clear();
        tempTCArr.clear();
        tempACArr.clear();
        vaporAbv.clear();
        bottleNum.clear();
        corrAbv.clear();
        boilerTemp.clear();
        bottleAbv.clear();
        //                abvUncorr.clear();
        //                distTemp.clear();
        //                dateTime.clear();
        //                timeS.clear();
        //                tempTFArr.clear();
        //                tempAFArr.clear();
    }

    public ArrayList<Double> getVaporAbv() {
        return vaporAbv;
    }

    public ArrayList<Double> getVaporAbvCorr() {
        return vaporAbvCorr;
    }

    public Hashtable<String, Double> getBottleAbv() {
        return bottleAbv;
    }

    public ArrayList<Double> getColumnC() {
        return tempTCArr;
    }

    public ArrayList<Double> getKalmanCArr() {
        return kalmanCArr;
    }

    public ArrayList<Double> getmilliTime() {
        return milliTimeArr;
    }

    public void setTimeSpan(String timeSpan) {
        this.timeSpan = timeSpan;
    }

    public String getTimeSpan() {
        return timeSpan;
    }

}
